(
    function (factory) {
        if (typeof define === "function" && define.amd) {

            // AMD. Register as an anonymous module.
            define(["jquery", "./version"], factory);
        } else {

            // Browser globals
            factory(jQuery);
        }
    }
        (
        function ($) {
            //select function 
            function Select(array) {
                this.array = array;
                this.indexName = '';
                this.obj = '';
                //设置子SELECT
                // 参数：当前onchange的SELECT ID，要设置的SELECT ID
                this.subSelectChange = function (selectName1, selectName2) {
                    var obj1 = document.all[selectName1];
                    var obj2 = document.all[selectName2];
                    var objName = this.toString();
                    var me = this;
                    obj1.onchange = function () {
                        me.optionChange(this.options[this.selectedIndex].value, obj2.id)
                    }
                }
                //设置第一个SELECT
                // 参数：indexName指选中项,selectName指select的ID
                this.firstSelectChange = function (indexName, selectName) {
                    this.obj = document.all[selectName];
                    this.indexName = indexName;
                    this.optionChange(this.indexName, this.obj.id)
                }
                this.optionChange = function (indexName, selectName) {
                    var obj1 = document.all[selectName];
                    var me = this;
                    obj1.length = 0;
                    obj1.options[0] = new Option("请选择", '');
                    for (var i = 0; i < this.array.length; i++) {
                        if (this.array[i][1] == indexName) {
                            obj1.options[obj1.length] = new Option(this.array[i][2], this.array[i][0]);
                        }
                    }
                }
            }
            window.onload = function () {
                var array = new Array();
                //tongbi--------------------------------------------
                //这是调用代码
                $.get("mockdata/select.json").done(
                    function (data) {
                        var select = new Select(data) //设置数据源
                        select.firstSelectChange("根目录", "s1"); //设置第一个选择框
                        select.subSelectChange("s1", "s2"); //设置子级选择框
                        select.subSelectChange("s2", "s3");
                        select.subSelectChange("s3", "s4");
                    }
                );
                //huanbi-------------------------------------------------------------
                //--------------------------------------------
                //这是调用代码

                $.get("mockdata/select.json").done(
                    function (data) {
                        var select2 = new Select(data);
                        //设置第一个选择框
                        select2.firstSelectChange("根目录", "x1");
                        //设置子选择框
                        select2.subSelectChange("x1", "x2")
                        select2.subSelectChange("x2", "x3")
                        select2.subSelectChange("x3", "x4")
                    }
                )

            }
            // echart    
            var TestClass = function () {
                function init() {
                    var mycharts = echarts.init(document.getElementById("main"));
                    var mycharts1 = echarts.init(document.getElementById("main1"));
                    var charts_lighting = echarts.init(document.getElementById("sys-lighting"))
                    var charts_air = echarts.init(document.getElementById("sys-air"))

                    //同比分析 
                    $.get('mockdata/analysis.json').done(function (data) {
                        var xData = [];
                        var yData = [];
                        var yData1 = [];
                        var day1;
                        var day2;

                        for (i = 0; i < data.regions[0].buildings[0].floors[0].Rooms[0].days[0].value.length; i++) {
                            xData.push(data.regions[0].buildings[0].floors[0].Rooms[0].days[0].value[i].timestamp);
                            yData.push(data.regions[0].buildings[0].floors[0].Rooms[0].days[0].value[i].value)   
                        }
                        day1 = data.regions[0].buildings[0].floors[0].Rooms[0].days[0].day;

                        for (j = 0; j < data.regions[0].buildings[0].floors[0].Rooms[0].days[1].value.length; j++) {
                            yData1.push(data.regions[0].buildings[0].floors[0].Rooms[0].days[1].value[j].value)
                        }
                        day2 = data.regions[0].buildings[0].floors[0].Rooms[0].days[1].day;
                        mycharts.setOption({
                            title: {
                                text: ''
                            },

                            tooltip: {
                                trigger: 'axis'
                            },
                            legend: {
                                data: [day1, day2]
                            },
                            xAxis: {
                                type: 'category',
                                boundaryGap: false,
                                data: xData
                            },
                            yAxis: {
                                type: 'value',
                                axisLabel: {
                                    formatter: '{value} 度'
                                }
                            },
                            series: [
                                {
                                    name: day1,
                                    type: 'line',
                                    data: yData,
                                    markPoint: {
                                        data: [
                                            { type: 'max', name: '' },
                                            { type: 'min', name: '最小值' }
                                        ]
                                    },
                                    markLine: {
                                        data: [
                                            { type: 'average', name: '平均值' }
                                        ]
                                    }
                                },
                                {
                                    name: day2,
                                    type: 'line',
                                    data: yData1,
                                    markPoint: {
                                        data: [
                                            { name: '' }
                                        ]
                                    },
                                    markLine: {
                                        data: [
                                            { type: 'average', name: '平均值' },
                                            [{
                                                symbol: 'none',
                                                x: '90%',
                                                yAxis: 'max'
                                            }, {
                                                symbol: 'circle',
                                                label: {
                                                    normal: {
                                                        position: 'start',
                                                        formatter: '最大值'
                                                    }
                                                },
                                                type: 'max',
                                                name: '最高点'
                                            }]
                                        ]
                                    }
                                }
                            ]
                        })
                    });
                    $.get('mockdata/mockdata_compare.json').done(function (data) {
                        var xData_light = [];
                        var yData_light = [];
                        var yData_light1 = [];
                        var time0;
                        var time1;
                        for (f = 0; f < data.regions[0].buildings[0].floors[0].Rooms[0].days[0].value.length; f++) {
                            xData_light.push(data.regions[0].buildings[0].floors[0].Rooms[0].days[0].value[f].timestamp);
                            yData_light.push(data.regions[0].buildings[0].floors[0].Rooms[0].days[0].value[f].lightingSystem)
                        }
                        time0 = data.regions[0].buildings[0].floors[0].Rooms[0].days[0].day;

                        for (f = 0; f < data.regions[0].buildings[0].floors[0].Rooms[0].days[1].value.length; f++) {
                            yData_light1.push(data.regions[0].buildings[0].floors[0].Rooms[0].days[1].value[f].lightingSystem)
                        }
                        time1 = data.regions[0].buildings[0].floors[0].Rooms[0].days[1].day

                        charts_lighting.setOption({
                            tooltip: {
                                trigger: 'axis',
                                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                                    type: 'line'        // 默认为直线，可选为：'line' | 'shadow'
                                }
                            },
                            xAxis: {
                                type: 'category',
                                boundaryGap: false,
                                data: xData_light
                            },
                            yAxis: {
                                type: 'value',
                                axisLabel: {
                                    formatter: '{value} 度'
                                }
                            },
                            legend: {
                                data: [time0, time1]
                            },
                            series: [
                                {
                                    type: 'line',
                                    name: time0,
                                    data: yData_light,
                                    markPoint: {
                                        data: [
                                            { type: 'max', name: '' },
                                            { type: 'min', name: '最小值' }
                                        ]
                                    },
                                    markLine: {
                                        data: [
                                            { type: 'average', name: '平均值' }
                                        ]
                                    }

                                },
                                {
                                    type: 'line',
                                    name: time1,
                                    data: yData_light1,
                                    markPoint: {
                                        data: [
                                            { name: '' }
                                        ]
                                    },
                                    markLine: {
                                        data: [
                                            { type: 'average', name: '平均值' },
                                            [{
                                                symbol: 'none',
                                                x: '90%',
                                                yAxis: 'max'
                                            }, {
                                                symbol: 'circle',
                                                label: {
                                                    normal: {
                                                        position: 'start',
                                                        formatter: '最大值'
                                                    }
                                                },
                                                type: 'max',
                                                name: '最高点'
                                            }]
                                        ]
                                    }
                                }
                            ]
                        });
                    });
                    $.get('mockdata/mockdata_compare.json').done(function (data) {
                        var xData_light = [];
                        var yData_light = [];
                        var yData_light1 = [];
                        var time0;
                        var time1;

                        for (f = 0; f < data.regions[0].buildings[0].floors[0].Rooms[0].days[0].value.length; f++) {
                            xData_light.push(data.regions[0].buildings[0].floors[0].Rooms[0].days[0].value[f].timestamp);
                            yData_light.push(data.regions[0].buildings[0].floors[0].Rooms[0].days[0].value[f].airSystem)
                        }
                        time0 = data.regions[0].buildings[0].floors[0].Rooms[0].days[0].day;

                        for (f = 0; f < data.regions[0].buildings[0].floors[0].Rooms[0].days[1].value.length; f++) {
                            yData_light1.push(data.regions[0].buildings[0].floors[0].Rooms[0].days[1].value[f].airSystem)
                        }
                        time1 = data.regions[0].buildings[0].floors[0].Rooms[0].days[1].day

                        charts_air.setOption({
                            tooltip: {
                                trigger: 'axis',
                                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                                    type: 'line'        // 默认为直线，可选为：'line' | 'shadow'
                                }
                            },
                            xAxis: {
                                type: 'category',
                                boundaryGap: false,
                                data: xData_light
                            },
                            yAxis: {
                                type: 'value',
                                axisLabel: {
                                    formatter: '{value} 度'
                                }
                            },
                            legend: {
                                data: [time0, time1]
                            },
                            series: [
                                {
                                    type: 'line',
                                    name: time0,
                                    data: yData_light,
                                    markPoint: {
                                        data: [
                                            { type: 'max', name: '' },
                                            { type: 'min', name: '最小值' }
                                        ]
                                    },
                                    markLine: {
                                        data: [
                                            { type: 'average', name: '平均值' }
                                        ]
                                    }

                                },
                                {
                                    type: 'line',
                                    name: time1,
                                    data: yData_light1,
                                    markPoint: {
                                        data: [
                                            { name: '' }
                                        ]
                                    },
                                    markLine: {
                                        data: [
                                            { type: 'average', name: '平均值' },
                                            [{
                                                symbol: 'none',
                                                x: '90%',
                                                yAxis: 'max'
                                            }, {
                                                symbol: 'circle',
                                                label: {
                                                    normal: {
                                                        position: 'start',
                                                        formatter: '最大值'
                                                    }
                                                },
                                                type: 'max',
                                                name: '最高点'
                                            }]
                                        ]
                                    }
                                }
                            ]
                        });
                    });
                    // 对比分析
                    var option1 = {
                        tooltip: {
                            trigger: 'axis',
                            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                                type: 'line'        // 默认为直线，可选为：'line' | 'shadow'
                            }
                        },
                        legend: {
                            data: ['空调系统', '照明系统', '电梯系统']
                        },
                        grid: {
                            left: '3%',
                            right: '4%',
                            bottom: '3%',
                            containLabel: true
                        },
                        xAxis: [
                            {
                                type: 'category',
                                data: ['A区', 'B区', 'C区', 'D区']
                            }
                        ],
                        yAxis: [
                            {
                                type: 'value',
                                axisLabel: {
                                    formatter: '{value}度'
                                }

                            }
                        ],
                        series: [
                            {
                                name: '照明系统',
                                type: 'bar',
                                stack: '总量',
                                label: {
                                    normal: {
                                        show: false,
                                    }
                                },
                                data: [420, 432, 401, 434]
                            },
                            {
                                name: '空调系统',
                                type: 'bar',
                                stack: '总量',
                                label: {
                                    normal: {
                                        show: false
                                    }
                                },
                                data: [220, 182, 191, 234]
                            },
                            {
                                name: '电梯系统',
                                type: 'bar',
                                barWidth: '42%',
                                stack: '总量',
                                label: {
                                    normal: {
                                        show: false
                                    }
                                },
                                data: [320, 282, 291, 334]
                            }
                        ]
                    };
                    mycharts1.setOption(option1);

                }
                this.init = init;
            }

            var testInit = function () {
                var test = new TestClass();
                test.init();
            }
            $(testInit);

            // 环比分析
            var timeData,seriesData;
            
            $.get('mockdata/drilldown.json').done(
                function (data) {
                    timeData = data.data;
                    seriesData = data.series
                });
            $(function () {
                // Create the chart
                Highcharts.chart('container', {
                    chart: {
                        type: 'column'
                    },
                    title: {
                        text: '能耗环比分析'

                    },
                    xAxis: {
                        type: 'category'
                    },
                    yAxis: {
                        title: {
                            text: '用电量（kw*h）'
                        }
                    },
                    legend: {
                        enabled: false
                    },
                    plotOptions: {
                        series: {
                            borderWidth: 0,
                            dataLabels: {
                                enabled: true,
                                format: '{point.y:.1f}'
                            }
                        }
                    },
                    tooltip: {
                        headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
                        pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}</b> <br/>'
                    },
                    series: [{
                        name: '上一级',
                        colorByPoint: true,
                        data: timeData                      
                    }],
                    drilldown: {
                        series: seriesData
                    }
            });
        });
        }))